package com.dev;
import android.widget.LinearLayout;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.TextView;
import android.view.View;
import android.view.Gravity;
import android.content.pm.ShortcutManager;
import java.util.List;
import android.content.pm.ShortcutInfo;
import com.TextMore;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.graphics.Point;
import android.text.TextPaint;

public class QueryManageShortcut extends LinearLayout implements OnClickListener{
    MainActivity ma;
    WindowManager.LayoutParams wlp=new WindowManager.LayoutParams();
    LinearLayout titlebar;
    LayoutParams listlp;
    TextMore tm1;
    ListView list;
    String s1=" 显示桌面快捷方式";
    String s2=" 显示图标快捷方式";
    String s;
    TextView title;
    TextView bt1;
    IconViewClose close;
    ShowSoftInput ssi;
    List<ShortcutInfo> pin;
    List<ShortcutInfo> dyn;
    List<ShortcutInfo> willshow;
    int itemHeight;
    int divider=1;
    int dividerColor=0x77888888;
    public QueryManageShortcut(MainActivity ma){
        super(ma);
        this.ma=ma;
        setOrientation(1);
        setBackground(new GUI.BorderDrawable());
        bt1=new TextView(ma);
        bt1.setGravity(Gravity.CENTER);
        bt1.setOnClickListener(this);
        bt1.setText(" 删除 ");
        bt1.setTextSize(1,13);
        bt1.setTextColor(0xff00bbff);
        close=new IconViewClose(ma);
        close.setOnClickListener(this);
        tm1=new TextMore(ma);
        tm1.setOnClickListener(this);
        tm1.setText(s1);
        
        list=new ListView(ma);
        title=new TextView(ma);
        title.setGravity(Gravity.CENTER_VERTICAL);
        title.setText(" 管理快捷方式");
        title.setTextSize(1,13);
        titlebar=new LinearLayout(ma);
        titlebar.setBackground(new GUI.BorderDrawable());
        titlebar.setOrientation(0);
        titlebar.addView(title,new LayoutParams(0,-1,1f));
        titlebar.addView(close);
        addView(titlebar,-1,ma.dialogTitleBarHeight);
        addView(tm1,-1,ma.dialogRowHeight);
        addView(list);
        addView(bt1,-2,ma.dialogRowHeight);
    }
    public void show(){
        wlp.width=ma.wp*2/3;
        wlp.height=-1;
        if(ma.shortcutManager==null)ma.shortcutManager=ma.getSystemService(ShortcutManager.class);
        itemHeight=(int)(33*ma.dp);
        listlp=new LayoutParams(-1,itemHeight*3);
        updateViewLayout(list,listlp);
        if(s==null||s==s1){
            willshow=ma.shortcutManager.getPinnedShortcuts();
        }else{
            willshow=ma.shortcutManager.getDynamicShortcuts();
        }
        ma.wm.addView(this,wlp);
    }

    @Override
    public void onClick(View v) {
        if(v==close){
            ma.wm.removeView(this);
        }else if(v==tm1){
            if(s==null||s==s1){
                s=s2;
                willshow=ma.shortcutManager.getDynamicShortcuts();
                tm1.setText(s2);
                list.invalidate();
            }else{
                s=s1;
                willshow=ma.shortcutManager.getPinnedShortcuts();
                tm1.setText(s1);
                list.invalidate();
            }
        }
    }
    class ListView extends View{
        Paint paint=new Paint();
        ListView(MainActivity ma){
            super(ma);
            fingerMovedLim=(int)(4*ma.dp);
        }
        int fingerMovedOrientation;
        int fingerMovedLim;
        int scrollYSaved;
        Point touchDown=new Point();
        Point touchSaved=new Point();
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            int ex=(int)event.getX();
            int ey=(int)event.getY();
            switch(event.getAction()){
                case MotionEvent.ACTION_DOWN:
                    scrollYSaved=getScrollY();
                    touchDown.x=ex;
                    touchDown.y=ey;
                    touchSaved.x=ex;
                    touchSaved.y=ey;
                    break;
                case MotionEvent.ACTION_MOVE:
                    if(fingerMovedOrientation==0){
                        int dxabs=Math.abs(ex-touchDown.x);
                        int dyabs=Math.abs(ey-touchDown.y);
                        if(dxabs>dyabs){
                            if(dxabs>fingerMovedLim){
                                fingerMovedOrientation=2;
                            }
                        }else if(dyabs>fingerMovedLim){
                            fingerMovedOrientation=1;
                        }
                    }else if(fingerMovedOrientation==1||fingerMovedOrientation==2){
                        if(willshow==null){
                            setScrollY(0);
                        }else{
                            int dy=ey-touchSaved.y;
                            int scrollYWill=scrollYSaved-dy;
                            int vh=getHeight();
                            int ch=itemHeight*willshow.size();
                            if(scrollYWill<0){
                                setScrollY(0);
                                scrollYSaved=0;
                                touchSaved.x=ex;
                                touchSaved.y=ey;
                            }else if(ch<=vh){
                                setScrollY(0);
                                scrollYSaved=0;
                                touchSaved.x=ex;
                                touchSaved.y=ey;
                            }else if(scrollYWill>ch-vh){
                                setScrollY(ch-vh);
                                scrollYSaved=ch-vh;
                                touchSaved.x=ex;
                                touchSaved.y=ey;
                            }else{
                                setScrollY(scrollYWill);
                            }
                        }
                    }
                    break;
                case MotionEvent.ACTION_UP:
                    if(fingerMovedOrientation==0){
                        if(willshow!=null){
                            int pos=(getScrollY()+ey)/itemHeight;
                            if(pos>=0&&pos<willshow.size()){
                                
                            }
                        }
                    }
                    fingerMovedOrientation=0;
                    break;
                case MotionEvent.ACTION_CANCEL:
                    fingerMovedOrientation=0;
                    
            }
            return true;
        }
        
        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            if(willshow==null){
                paint.setTextSize((int)(14*ma.dp));
                paint.setColor(0x77888888);
                canvas.drawText("Data is null",10,paint.getTextSize(),paint);
            }else{
                int vw=getWidth();
                int vh=getHeight();
                int scroll=getScrollY();
                int size=willshow.size();
                int startItem=scroll/itemHeight;
                if(startItem<0){
                    startItem=0;
                }else if(startItem>=size){
                    return;
                }
                paint.setTextSize((int)(13*ma.dp));
                int padding=(int)(10*ma.dp);
                for(int yy=startItem*itemHeight,pos=startItem;pos<size&&yy<vh;pos++,yy+=itemHeight){
                    paint.setColor(dividerColor);
                    canvas.drawRect(0,yy+itemHeight-divider,vw,yy+itemHeight,paint);
                    paint.setColor(0xff000000);
                    int texty=yy+(itemHeight-(int)paint.getTextSize())/2+(int)-paint.ascent();
                    ShortcutInfo si=willshow.get(pos);
                    canvas.drawText(si.getShortLabel().toString(),texty,padding,paint);
                    
                }
            }
        }
    }
}
